'use strict';

const Controller = require('egg').Controller;

class VehicleApplicationController extends Controller {
  // 发起公车申请
  async createApplication() {
    const {
      ctx
    } = this;
    // 从请求体中获取参数
    const params = ctx.request.body;
    // 调用 service 层的方法处理申请
    const result = await ctx.service.vehicleApplication.createApplication(params);
    ctx.body = result;
  }

  // 获取申请列表
  async getApplicationList() {
    const {
      ctx
    } = this;
    // 从请求体中获取参数
    const params = ctx.request.body;
    // 调用 service 层的方法获取列表
    const result = await ctx.service.vehicleApplication.getApplicationList(params);
    ctx.body = result;
  }

  // 查询可用车辆信息
  async getAvailableVehicles() {
    const {
      ctx
    } = this;
    const result = await ctx.service.vehicleApplication.getAvailableVehicles();
    ctx.body = result;
  }

  // 查询可用驾驶员信息
  async getAvailableDrivers() {
    const {
      ctx
    } = this;
    const result = await ctx.service.vehicleApplication.getAvailableDrivers();
    ctx.body = result;
  }

  // 根据申请单 ID 获取申请单的所有信息
  async getApplicationDetails() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    const result = await ctx.service.vehicleApplication.getApplicationDetails(params);
    ctx.body = result;
  }

  // 取消用车申请
  async cancelApplication() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    const result = await ctx.service.vehicleApplication.cancelApplication(params);
    ctx.body = result;
  }

  // 审核用车申请
  async reviewApplication() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    const result = await ctx.service.vehicleApplication.reviewApplication(params);
    ctx.body = result;
  }

  /**
   * 根据车辆 ID 获取分配给该车辆的所有申请单信息
   */
  async getApplicationsByVehicleId() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    const result = await ctx.service.vehicleApplication.getApplicationsByVehicleId(params);
    ctx.body = result;
  }

  // 根据驾驶员 ID 获取分配给该驾驶员的所有申请单信息
  async getApplicationsByDriverId() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    const result = await ctx.service.vehicleApplication.getApplicationsByDriverId(params);
    ctx.body = result;
  }

  // 获取日常记录
  async getVehicleDailyRecords() {
    const {
      ctx
    } = this;
    const {
      year_month
    } = ctx.request.body;

    try {
      const result = await this.service.vehicleApplication.getVehicleDailyRecords({
        year_month
      });
      ctx.body = result;

    } catch (error) {
      ctx.throw(422, error.message)
    }
  }

  // 处理新增车辆每日记录的请求
  async createVehicleDailyRecord() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    const result = await ctx.service.vehicleApplication.createVehicleDailyRecord(params);
    ctx.body = result;
  }

  // 处理编辑车辆每日记录的请求
  async updateVehicleDailyRecord() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    const result = await ctx.service.vehicleApplication.updateVehicleDailyRecord(params);
    ctx.body = result;
  }

  // 处理删除车辆每日记录的请求
  async deleteVehicleDailyRecord() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    const result = await ctx.service.vehicleApplication.deleteVehicleDailyRecord(params);
    ctx.body = result;
  }
}

module.exports = VehicleApplicationController;